package com.xinzhi.service.impl.recordImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinzhi.mapper.IncomeAndExpensesMapper;
import com.xinzhi.pojo.Incomeandexpenditurerecord;
import com.xinzhi.pojo.dto.Abbreviation;
import com.xinzhi.pojo.dto.ShouZhi;
import com.xinzhi.service.recordService.IIncomeAndExpensesService;
import com.xinzhi.utils.Result;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Stream;

@Service
public class IncomeAndExpensesServiceImpl implements IIncomeAndExpensesService {
    @Autowired
    private IncomeAndExpensesMapper incomeAndExpensesMapper;

//    @Override
//    public Integer addIncomeAndExpenses(Incomeandexpenditurerecord incomeandexpenditurerecord) {
//        return null;
//    }

    @Override
    public Result selectIncomeAndExpenses(Integer pageNum, Integer pageSize,Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Abbreviation> abbreviations = incomeAndExpensesMapper.selectAbbreviationByUserId(userId);
        abbreviations.sort((a, b) -> b.getTradingHour().compareTo(a.getTradingHour()));
        System.out.println(abbreviations);
        PageInfo<Abbreviation> pageInfo = new PageInfo<Abbreviation>(abbreviations);
        return Result.ok(pageInfo);
    }

    /**
     * 根据收支情况及金钱范围进行查询
     * @param shouZhi
     * @return
     */
    @Override
    public Result selectBytype(ShouZhi shouZhi) {
        PageHelper.startPage(shouZhi.getPageNum(), shouZhi.getPageSize());
        QueryWrapper queryWrapper =new QueryWrapper();
       if (shouZhi.getIncome() != null){
           if (shouZhi.getIncome() >0){
               queryWrapper.ge("account_amount",0);
           }
           if (shouZhi.getIncome() <0){
               queryWrapper.le("account_amount",0);
           }
       }
       queryWrapper.eq("user_id",shouZhi.getUserId());
        if (shouZhi.getStart() != null && shouZhi.getEnd() != null){
            queryWrapper.between("amount",shouZhi.getStart(),shouZhi.getEnd());
        }
        List<Incomeandexpenditurerecord> list = incomeAndExpensesMapper.selectList(queryWrapper);
        System.out.println(list);
        if (list.isEmpty()){
            return Result.error("没有找到相关记录");
        }
        list.sort((a,b)->b.getTradingHour().compareTo(a.getTradingHour()));
        PageInfo<Incomeandexpenditurerecord> pageInfo =new PageInfo<>(list);
        return Result.ok(pageInfo);
    }
    /**
     * 根据卡号进行查询
     * @param zhi
     * @return
     */
    @Override
    public Result selectByCard(ShouZhi zhi) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("transaction_card",zhi.getTransactionCard());
        List<Incomeandexpenditurerecord> list = incomeAndExpensesMapper.selectList(queryWrapper);
        if (list.isEmpty()){
            return Result.error("没有找到相关记录");
        }
        list.sort((a,b)->b.getTradingHour().compareTo(a.getTradingHour()));
        PageInfo<Incomeandexpenditurerecord> pageInfo =new PageInfo<>(list);
        return Result.ok(pageInfo);
    }
    /**
     * 根据时间进行查询
     * @param time
     * @param time2
     * @param userId
     * @return
     * @throws ParseException
     */
    @Override
    public Result selectByTime(String time,String time2 ,Integer userId) throws ParseException {
        PageHelper.startPage(1, 10);
        Date start =null;
        Date end =null;
        System.out.println(time2);
        if (time2 == ""){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            Date startDate = sdf.parse(time);
            // 设置开始时间为该月的第一天
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
             start = calendar.getTime();

            // 设置结束时间为该月的最后一天
            calendar.add(Calendar.MONTH, 1); // 移动到下个月
            calendar.set(Calendar.DAY_OF_MONTH, 0); // 设置为上个月的最后一天
             end = calendar.getTime();
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            start = sdf.parse(time);
            end = sdf.parse(time2);
        }
        // 解析日期字符串
        Map<String, Object> params = new HashMap<>();
        params.put("start", start);
        params.put("end", end);
        params.put("userId", userId);
        List<Incomeandexpenditurerecord> list = incomeAndExpensesMapper.selectByTime(params);
        if (list.isEmpty()){
            return Result.error("没有找到相关记录");
        }
        list.sort((a,b)->b.getTradingHour().compareTo(a.getTradingHour()));
        PageInfo<Incomeandexpenditurerecord> pageInfo =new PageInfo<>(list);
        return Result.ok(pageInfo);
    }
}